package component.controls
{
	import component.controls.ui.Component;
	import component.global.ComponentSetting;
	import component.global.manager.EventMgr;
	import component.global.utils.DisplayUtils;
	import component.global.utils.FilterUtil;
	import component.global.utils.GraphicsUtils;
	import component.global.utils.ObjectUtil;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class Frame extends Component
	{
		public function Frame($title:String = ComponentSetting.FRAME_DEFAULT_TITLE,$width:int = ComponentSetting.FRAME_DEFAULT_WIDTH, $height:int = ComponentSetting.FRAME_DEFAULT_HEIGHT)
		{
			_title = $title;
			_width = $width;
			_height = $height;
			super();
		}
		
		protected var _title:String;
		protected var _titleTextField:Label;
		protected var _titleBackground:Shape;
		protected var _buttonBackground:Shape;
		protected var _summitButton:Button;
		protected var _cancelButton:Button;
		protected var _closeButton:Button;
		protected var _container:Sprite;
		
		override protected function init():void
		{
			_background = GraphicsUtils.createRectangle(_width,_height);
			_titleBackground = GraphicsUtils.createRectangle(_width - 2, 20);
			_buttonBackground = GraphicsUtils.createRectangle(_width - 2,28);
			_titleTextField = new Label(_title);
			_summitButton = new Button(ComponentSetting.BUTTON_SUMMIT_LABEL,_width/5,20);
			_cancelButton = new Button(ComponentSetting.BUTTON_CANCEL_LABEL,_width/5,20);
			_closeButton = new Button("×",19,19);
			_container = new Sprite();
			
			_background.filters = [FilterUtil.createDropShadowFilter(45),FilterUtil.createDropShadowFilter(45,true)];
			_titleBackground.filters = [FilterUtil.createDropShadowFilter(90,false,1)];
			_titleBackground.x = 1;
			_titleBackground.y = 2;
			_buttonBackground.filters = [FilterUtil.createDropShadowFilter(270,false,1)];
			_buttonBackground.x = 1;
			_buttonBackground.y = _height - _buttonBackground.height;
			_titleTextField.x = (_background.width - _titleTextField.width)/2;
			_titleTextField.y = 2;
			_summitButton.x = _width/5;
			_cancelButton.x = _summitButton.x + 2 * _width/5;
			_summitButton.y = _cancelButton.y = _background.height - _summitButton.height - 4;
			_closeButton.x = _background.width - _closeButton.width/2;
			_closeButton.y = -(_closeButton.height/2);
			_container.x = 10;
			_container.y = 30;
			
			EventMgr.Instance.addEvent(_closeButton, [MouseEvent.CLICK], [__onFuncBtnClick]);
			EventMgr.Instance.addEvent(_summitButton, [MouseEvent.CLICK], [__onFuncBtnClick]);
			EventMgr.Instance.addEvent(_cancelButton, [MouseEvent.CLICK], [__onFuncBtnClick]);
			
			EventMgr.Instance.addEvent(this,[MouseEvent.MOUSE_DOWN,MouseEvent.MOUSE_UP],[__onMouseDown,__onMouseUp]);
			DisplayUtils.addDisplayObject(this,_background,_titleBackground,_buttonBackground,_titleTextField,_summitButton,_cancelButton,_closeButton,_container);
		}
		
		public function addContent(content:DisplayObject):DisplayObject{
			return _container.addChild(content);
		}
		
		protected function __onFuncBtnClick(e:MouseEvent):void{
			dispose();
		}
		
		protected function __onMouseDown(e:MouseEvent):void{
			if(e.target == this){
				this.startDrag();
			}
		}
		
		protected function __onMouseUp(e:MouseEvent):void{
				this.stopDrag();
		}
		
		override public function dispose():void
		{
			super.dispose();
			EventMgr.Instance.removeEvent(this);
			ObjectUtil.dispose(_titleTextField);_titleTextField=null;
			ObjectUtil.dispose(_titleBackground);_titleBackground=null;
			ObjectUtil.dispose(_buttonBackground);_buttonBackground=null;
			ObjectUtil.dispose(_summitButton);_summitButton=null;
			ObjectUtil.dispose(_cancelButton);_cancelButton=null;
			ObjectUtil.dispose(_closeButton);_closeButton=null;
			ObjectUtil.dispose(_container);_container=null;
		}
	}
}